﻿using System;
using System.Collections.Generic;
using System.Web;
using WAD.WEB.AppCode;
using WAD.Model;
using WAD.DAL;
using System.Text;
using WAD.Model.JSON.Order;
using WAD.BLL;
namespace WAD.WEB.Client2.Ajax
{
    /// <summary>
    /// 订单接口
    /// </summary>
    public partial class FC_Order : AjaxBase
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "addorder": this.AddOrder(); break;
                case "sysaddorder": this.SysAddOrder(); break;
                case "getorder": this.GetOrder(); break;
            }
        }
        [QD.Web.Ajax.AjaxMethod]
        public void GetOrder()
        {
            var orderid = Parse.String("orderid", "");

            if (string.IsNullOrEmpty(orderid))
                JsonResult(11, "orderid不能为空");

            var order = DAOFCOrder.Get(orderid);
            if (order != null)
                Success(order);
            else
                Fail();


        }

        protected object lockObj = new object();
        /// <summary>
        /// 终端支付：创建在线支付订单(广告主充值、广告发布 都调用此接口)
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void AddOrder()
        {
            IsLogin();
            MFCOrder m = new MFCOrder()
            {
                user_id = Parse.Int("user_id", 0),
                article_id = Parse.Int("article_id", 0),
                total_price = Parse.Decimal("total_price", 0),
                pay_price = Parse.Decimal("pay_price", 0),
                unit_price = Parse.Decimal("unit_price", 0),
                number = Parse.Int("number", 0),
                platform = Parse.Int("platform", 0),
                state = 0,
                date = DateTime.Now
            };
            if (m.user_id < 1)
                JsonResult(11, "会员无效");

            if (m.number < 1)
                JsonResult(11, "订单数量错误");

            if (m.article_id > 0)
            {
                if (m.unit_price < 1)
                    JsonResult(11, "金额不能低于1元");

                if (m.number < 100)
                {

                    JsonResult(11, "数量不能小于100");

                }
            }

            m.after_price = m.total_price * Convert.ToDecimal(0.1);    //到账金额
            bool isAdd = false;
            lock (lockObj)
            {
                m.agentid = userBean.agentid;
                m.orderid = FCOrderBLL.CreateId(m.user_id);
                isAdd = DAOFCOrder.Add(m);
                //(article_id=0，表示只充值. >0表示 既充值，又生成广告)
                if (m.article_id > 0)
                {
                    DAOFCSharedMoney.Add_HongBao2(m.article_id, m.number); //生成红包
                }
            }
            if (isAdd)
            {
                MOrder o = new MOrder();
                o.orderid = m.orderid;  //订单号
                if (m.platform == 1)
                    o.notify_url = string.Format("http://{0}/client2/api/wx_notify.aspx", Request.Url.Authority);    //微信成功的回调地址
                else if (m.platform == 2)
                    o.notify_url = string.Format("http://{0}/client2/api/alipay_notify.aspx", Request.Url.Authority);//支付宝成功的回调地址
                Success(o);
            }
            else
                Fail();
        }

        /// <summary>
        /// 终端支付：创建余额支付订单(广告主充值、广告发布 都调用此接口)
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void SysAddOrder()
        {
            IsLogin();
            MFCOrder m = GetModel();
            if (m.article_id < 1)
                ParamError();

            if (m.platform != 0)
                JsonResult(11, "支付方式错误");

            decimal pay_money = Parse.Decimal("pay_price", 0);
            if (pay_money > 0)
                JsonResult(11, "支付金额错误");

            decimal sys_price = Parse.Decimal("sys_price", 0);
            if (sys_price < 1)
                JsonResult(11, "请求无效,请刷新后在操作");

            if (sys_price + pay_money != m.total_price)
                JsonResult(11, "支付金额无效");

            if (m.unit_price * m.number != m.total_price)
                JsonResult(11, "支付金额有误差");

            bool isAdd = false;
            string msg = "";
            lock (lockObj)
            {
                if (m.unit_price < 1)
                    JsonResult(11, "金额不能低于1元");

                //单价，数量验证
                if (m.number < 100)
                {
                    JsonResult(11, "数量不能小于100");
                }

                m.agentid = userBean.agentid;
                m.orderid = FCOrderBLL.CreateId(m.user_id);
                isAdd = DAOFCOrder.Add(m);  //添加订单

                //订单结果处理
                msg = FCOrderBLL.Sys_CallBack2(m.orderid, Request.UserHostAddress, WJXConfig.EncryptConfig.DES_KEY);
            }
            if (isAdd && string.IsNullOrEmpty(msg))
                Success();
            Fail();
        }

        MFCOrder GetModel()
        {
            MFCOrder m = new MFCOrder()
            {
                user_id = Parse.Int("user_id", 0),
                article_id = Parse.Int("article_id", 0),
                total_price = Parse.Decimal("total_price", 0),
                pay_price = Parse.Decimal("pay_price", 0),
                unit_price = Parse.Decimal("unit_price", 0),
                number = Parse.Int("number", 0),
                platform = Parse.Int("platform", 0),
                state = 0,
                date = DateTime.Now
            };
            return m;
        }
    }
}